//
//  kata_IndexTitleViewController.m
//  CityStar
//
//  Created by wangjia on 14-6-13.
//  Copyright (c) 2014年 kata. All rights reserved.
//

#import "kata_IndexTitleViewController.h"
#import "HTDefine.h"
@interface kata_IndexTitleViewController ()

@end

@implementation kata_IndexTitleViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.btnLock addTarget:self action:@selector(changeLocal) forControlEvents:UIControlEventTouchUpInside];
    
    [self changeTitle];
    // Do any additional setup after loading the view from its nib.
}

-(void)changeLocal
{
    if ([self.delegate respondsToSelector:@selector(didSelectCitySender)]) {
        [self.delegate didSelectCitySender];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)changeLocalByCityName:(NSString *)cityName{
    [self.btnLock setTitle:cityName forState:UIControlStateNormal];

    if ([self.delegate respondsToSelector:@selector(changeLocalByCityName:)]) {
        [self.delegate changeLocalByCityName:cityName];
    }
}

- (void)changeTitle{
//    if ([[NSUserDefaults standardUserDefaults] objectForKey:SELECTED_LOCATION_KEY]) {
//        NSDictionary * locationData = [[NSUserDefaults standardUserDefaults] objectForKey:SELECTED_LOCATION_KEY];
//        NSMutableString * titleStr = [[NSMutableString alloc] initWithFormat:@"%@", [locationData objectForKey:@"name"]];
//        NSRange range = [titleStr rangeOfString:@"市"];
//        if (range.location != NSNotFound) {
//            [titleStr replaceCharactersInRange:range withString:@""];
//        }
//        NSString  *strTitle = [titleStr substringToIndex:2];
//        [self.btnLock setTitle:strTitle forState:UIControlStateNormal];
//    }else{
//        [self.btnLock setTitle:DEFAULT_LOCATION forState:UIControlStateNormal];
//    }
}

- (void)setAryFiveTitle:(NSMutableArray *)aryFiveTitle andCityID:(NSMutableArray *)aryFiveID{
    _aryFiveTitle = aryFiveTitle;
    
    _btnFirst.enabled = NO;
    _btnSecond.enabled = NO;
    _btnThird.enabled = NO;
    _btnForth.enabled = NO;
    _btnFifth.enabled = NO;
    [_btnFirst setTitle:@"" forState:UIControlStateNormal];
    [_btnSecond setTitle:@"" forState:UIControlStateNormal];
    [_btnThird setTitle:@"" forState:UIControlStateNormal];
    [_btnForth setTitle:@"" forState:UIControlStateNormal];
    [_btnFifth setTitle:@"" forState:UIControlStateNormal];
    
    if (aryFiveTitle.count > 0) {
        NSString *strName = [aryFiveTitle objectAtIndex:0] ;

//        NSString *strName = [[aryFiveTitle objectAtIndex:0] objectForKey:@"name"];
        strName = [strName substringToIndex:2];
        [_btnFirst setTitle:strName forState:UIControlStateNormal];
        _btnFifth.tag = [aryFiveID[0] intValue];
        _btnFirst.enabled = YES;
    }
    
    if (aryFiveTitle.count > 1) {
        NSString *strName = [aryFiveTitle objectAtIndex:1] ;

//        NSString *strName = [[aryFiveTitle objectAtIndex:1] objectForKey:@"name"];
        strName = [strName substringToIndex:2];
        [_btnSecond setTitle:strName forState:UIControlStateNormal];
        _btnFifth.tag = [aryFiveID[1] intValue];
        _btnSecond.enabled = YES;
    }
    
    if (aryFiveTitle.count > 2) {
        NSString *strName = [aryFiveTitle objectAtIndex:2] ;

//        NSString *strName = [[aryFiveTitle objectAtIndex:2] objectForKey:@"name"];
        strName = [strName substringToIndex:2];
        [_btnThird setTitle:strName forState:UIControlStateNormal];
        _btnFifth.tag = [aryFiveID[2] intValue];

        _btnThird.enabled = YES;
    }
    
    if (aryFiveTitle.count > 3) {
        NSString *strName = [aryFiveTitle objectAtIndex:3] ;

//        NSString *strName = [[aryFiveTitle objectAtIndex:3] objectForKey:@"name"];
        strName = [strName substringToIndex:2];
        [_btnForth setTitle:strName forState:UIControlStateNormal];
        _btnFifth.tag = [aryFiveID[3] intValue];

        _btnForth.enabled = YES;
    }
    
    if (aryFiveTitle.count > 4) {
        NSString *strName = [aryFiveTitle objectAtIndex:4] ;

//        NSString *strName = [[aryFiveTitle objectAtIndex:4] objectForKey:@"name"];
        strName = [strName substringToIndex:2];
        [_btnFifth setTitle:strName forState:UIControlStateNormal];
        _btnFifth.tag = [aryFiveID[4] intValue];

        _btnFifth.enabled = YES;
    }
    
    
    
//    if (aryFiveTitle.count > 4) {
//        for (int i = 0; i < aryFiveTitle.count; i++) {
//            NSString *strName = [[aryFiveTitle objectAtIndex:i] objectForKey:@"name"];
//            strName = [strName substringToIndex:2];
//            switch (i) {
//                case 0:
//                    [_btnFirst setTitle:strName forState:UIControlStateNormal];
//                    break;
//                case 1:
//                    [_btnSecond setTitle:strName forState:UIControlStateNormal];
//                    break;
//                case 2:
//                    [_btnThird setTitle:strName forState:UIControlStateNormal];
//                    break;
//                case 3:
//                    [_btnForth setTitle:strName forState:UIControlStateNormal];
//                    break;
//                case 4:
//                    [_btnFifth setTitle:strName forState:UIControlStateNormal];
//                    break;
//                    
//                default:
//                    break;
//            }
//        }
//    }
}

- (IBAction)changeLocaAction:(UIButton *)sender {
//    NSLog(@"%d",sender.tag);
//
    [self.btnLock setTitle:sender.titleLabel.text forState:UIControlStateNormal];
    [KStandUserDefaults setObject:[NSString stringWithFormat:@"%li",(long)sender.tag] forKey:KUserDefaultKey_PersonalCityID];
    [KStandUserDefaults setObject:[NSString stringWithFormat:@"%@",self.btnLock.titleLabel.text] forKey:KUserDefaultKey_PersonalCityName];
    [KStandUserDefaults synchronize];

    if ([self.delegate respondsToSelector:@selector(changeLockBtnName)]) {
        [self.delegate changeLockBtnName];
    }
//    NSDictionary * cityData = (NSDictionary *)[self.aryFiveTitle objectAtIndex:sender.tag-1];
//    NSString *telphone = [cityData objectForKey:@"telphone"];
//    [[NSUserDefaults standardUserDefaults] setObject:telphone forKey:@"telphone"];
//    [[NSUserDefaults standardUserDefaults] setObject:cityData forKey:SELECTED_LOCATION_KEY];
    
//    [[NSUserDefaults standardUserDefaults] synchronize];
	
//    if (self.lbsViewControllerDelegate && [self.lbsViewControllerDelegate respondsToSelector:@selector(didSelectedCity:)]) {
    
//        [self.lbsViewControllerDelegate didSelectedCity:[[NSUserDefaults standardUserDefaults] objectForKey:SELECTED_LOCATION_KEY]];
    
//    }
}


@end
